//
//  Person.m
//  Cabinet
//
//  Created by Florent Montsegur on 27/04/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "Person.h"


@implementation Person
@dynamic Commentaires;
@dynamic Nom;
@dynamic Naissance;
@dynamic Prenom;
@dynamic MedecinTraitant;
@dynamic Sexe;
@dynamic Consultations;
@dynamic Vaccinations;


- (void)addConsultationsObject:(NSManagedObject *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Consultations" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Consultations"] addObject:value];
    [self didChangeValueForKey:@"Consultations" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
}

- (void)removeConsultationsObject:(NSManagedObject *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Consultations" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Consultations"] removeObject:value];
    [self didChangeValueForKey:@"Consultations" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
}

- (void)addConsultations:(NSSet *)value {    
    [self willChangeValueForKey:@"Consultations" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Consultations"] unionSet:value];
    [self didChangeValueForKey:@"Consultations" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeConsultations:(NSSet *)value {
    [self willChangeValueForKey:@"Consultations" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Consultations"] minusSet:value];
    [self didChangeValueForKey:@"Consultations" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}

- (void)addVaccinationsObject:(NSManagedObject *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Vaccinations" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Vaccinations"] addObject:value];
    [self didChangeValueForKey:@"Vaccinations" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
}

- (void)removeVaccinationsObject:(NSManagedObject *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"Vaccinations" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"Vaccinations"] removeObject:value];
    [self didChangeValueForKey:@"Vaccinations" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
}

- (void)addVaccinations:(NSSet *)value {
    [self willChangeValueForKey:@"Vaccinations" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Vaccinations"] unionSet:value];
    [self didChangeValueForKey:@"Consultations" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeVaccinations:(NSSet *)value {
    [self willChangeValueForKey:@"Vaccinations" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"Vaccinations"] minusSet:value];
    [self didChangeValueForKey:@"Vaccinations" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}


- (NSInteger)calculAge
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
    NSDateComponents *dateComponentsNow = [calendar components:unitFlags fromDate:[NSDate date] ];
    NSDateComponents *dateComponentsBirth = [calendar components:unitFlags fromDate:[self Naissance]];
    
    if (([dateComponentsNow month] < [dateComponentsBirth month]) ||
        (([dateComponentsNow month] == [dateComponentsBirth month]) && ([dateComponentsNow day] < [dateComponentsBirth day]))) {
        return [dateComponentsNow year] - [dateComponentsBirth year] - 1;
    }
    
    return [dateComponentsNow year] - [dateComponentsBirth year];
}


@end
